from 数据库 import de_y_query
from PyQt5.QtWidgets import QApplication, QDialog, QTableWidgetItem
import pymysql
from PyQt5.QtCore import  Qt
import sys


class MainDialog(QDialog):
    def __init__(self, parent=None):
        super(QDialog, self).__init__(parent)
        self.ui = de_y_query.Ui_Dialog()
        self.ui.setupUi(self)

    def query(self):
        conn = pymysql.connect(host='127.0.0.1', user='root', passwd='dqzx2020', port=3306, db='design', charset='utf8')
        zonta = []
        name = self.ui.comboBox.currentText()
        cur = conn.cursor()
        if name[0:-2] == '员工':
            sql = "select * from {} where 员工姓名 = '{}'".format(name[0:-2], self.name)
        else:
            sql = "select * from {}".format(name[0:-2])
        cur.execute(sql)
        head = cur.description
        data = cur.fetchall()
        self.ui.tabWidget.setColumnCount(len(head))
        self.ui.tabWidget.setRowCount(len(data))
        for i in head:
            zonta.append(str(i[0]))
        self.ui.tabWidget.setHorizontalHeaderLabels(zonta)
        for i in range(0, len(data)):
            for j in range(0, len(data[i])):
                item = QTableWidgetItem(str(data[i][j]))
                item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
                item.setTextAlignment(Qt.AlignCenter | Qt.AlignBottom)
                self.ui.tabWidget.setItem(i, j, item)
        conn.close()

    def append(self, name):
        self.name = name
        self.ui.label.setText("<html><head/><body><p align=\"center\">欢迎您,{}员工</p></body></html>".format(self.name))
        self.show()



if __name__ == '__main__':
    myapp = QApplication(sys.argv)
    myDlg = MainDialog()
    myDlg.show()
    sys.exit(myapp.exec_())